package com.whut.www.dialogmenupopwindow;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

public class ContextMenuActivity extends AppCompatActivity {

    private ListView mListView;
    private ArrayAdapter<String> mAdapter;
    private ArrayList<String> mList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_context_menu);
        mListView = (ListView) findViewById(R.id.listView);
        mList = new ArrayList<>();
        for (int i = 0; i < 26; i++) {
            mList.add("message" + i);
        }
        mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mList);
        mListView.setAdapter(mAdapter);
        //注册上下文菜单
        registerForContextMenu(mListView);
    }

    /**
     * 当被注册的view，被长按，创建上下文菜单
     * 第一个参数：ContextMenu menu，上下文菜单对象
     * <p>
     * 第二个参数：View v
     * <p>
     * 第三个参数：ContextMenuInfo menuInfo，该对象携带了一些上下文菜单的信息
     */
    private int position;

    /**
     * @param menu
     * @param v
     * @param menuInfo 里面包含了一些额外的信息.(比如就有ListView等的position)
     */
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo
            menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        Log.i("ContextMenuActivity", "---onCreateContextMenu()--->" + "CreateContextMenu");
        //填充menu
        getMenuInflater().inflate(R.menu.context, menu);
        //menu添加icon
        //menu.setHeaderIcon(R.drawable.img01);
        //menu添加标题
        //menu.setHeaderTitle("带标题栏。。。");
        //View view = LayoutInflater.from(this).inflate(R.layout.activity_main, null);
        //menu添加自定义header
        // menu.setHeaderView(view);
        // 添加header在 getMenuInflater().inflate(R.menu.context, menu);后调用
        setMenuIconsVisible(menu);
        AdapterView.AdapterContextMenuInfo adapterContextMenuInfo = (AdapterView
                .AdapterContextMenuInfo) menuInfo;
        //获取创建上下文菜单的item
        position = adapterContextMenuInfo.position;
        Toast.makeText(this, String.valueOf(adapterContextMenuInfo.position) + "\t" +
                adapterContextMenuInfo.id, Toast.LENGTH_SHORT).show();
    }


    @Override
    public boolean onContextItemSelected(MenuItem item) {
        ContextMenu.ContextMenuInfo menuInfo = item.getMenuInfo();
        switch (item.getItemId()) {
            case R.id.insert:
                mList.add(position, "insert" + position);
                break;
            case R.id.delete:
                mList.remove(position);
                break;
            case R.id.upData:
                mList.set(position, "updata" + position);
                break;
        }
        mAdapter.notifyDataSetChanged();
        return super.onContextItemSelected(item);
    }

    private void setMenuIconsVisible(Menu menu) {
        try {
            //得到字节码
            Class<?> clazz = Class.forName("com.android.internal.view.menu.MenuBuilder");
            //根据字节码得到类中指定的方法                      方法名                     参数类型
            Method mMethod = clazz.getDeclaredMethod("setOptionalIconsVisible", boolean.class);
            //设置可访问性--->暴力反射.
            mMethod.setAccessible(true);
            //执行方法.
            mMethod.invoke(menu, true);
        } catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException |
                IllegalAccessException e) {
            e.printStackTrace();
        }
    }

}
